package com.ruoyi.labdanger.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.labdanger.domain.LabApplyPurchaseDetails;
import com.ruoyi.labdanger.service.ILabApplyPurchaseDetailsService;
import com.ruoyi.labdata.domain.LabFlowTask;
import com.ruoyi.labdata.domain.LabFlowTaskLog;
import com.ruoyi.labdata.service.ILabFlowTaskService;

/**
 * 危化品申购Controller
 * 
 * @author winner pan
 * @date 2022-03-04
 */
@RestController
@RequestMapping("/labdander/purchase_d")
public class LabApplyPurchaseDetailsController extends BaseController {
	@Autowired
	private ILabApplyPurchaseDetailsService labApplyPurchaseDetailsService;
	
	@Autowired
	private ILabFlowTaskService labFlowTaskService;
	/**
	 * 查询危化品申购列表
	 * labdata:casInventory:query
	 */
	@PreAuthorize("@ss.hasPermi('labdata:casInventory:query')")
	@GetMapping("/list")
	public TableDataInfo list(LabApplyPurchaseDetails labApplyPurchaseDetails) {
		startPage();
		if(labApplyPurchaseDetails.getPurchaseId()!=null){
			labApplyPurchaseDetails.setInstanceId(null);
			labApplyPurchaseDetails.setIsRemoved(0);
		}
		List<LabApplyPurchaseDetails> list = labApplyPurchaseDetailsService
				.selectLabApplyPurchaseDetailsList(labApplyPurchaseDetails);
		return getDataTable(list);
	}
	
	@PreAuthorize("@ss.hasPermi('labdanger:scanin:query')")
	@GetMapping("/listScandata")
	public TableDataInfo listScandata(LabApplyPurchaseDetails labApplyPurchaseDetails) {
		startPage();
		List<LabApplyPurchaseDetails> list = labApplyPurchaseDetailsService
				.queryScaninData(labApplyPurchaseDetails);
		return getDataTable(list);
	}
	
	@PreAuthorize("@ss.hasPermi('labdata:casInventory:query')")
	@GetMapping("/instance")
	public AjaxResult listByInstance(LabApplyPurchaseDetails labApplyPurchaseDetails) {
		List<LabApplyPurchaseDetails> list = labApplyPurchaseDetailsService
				.selectLabApplyPurchaseDetailsList(labApplyPurchaseDetails);
		return AjaxResult.success(list);
	}
	@PreAuthorize("@ss.hasPermi('labdata:casInventory:search')")
	@GetMapping("/inventory")
	public TableDataInfo queryInventory(LabApplyPurchaseDetails labApplyPurchaseDetails) {
		startPage();
		List<LabApplyPurchaseDetails> list = labApplyPurchaseDetailsService.queryInventory(labApplyPurchaseDetails);
		return getDataTable(list);
	}
	

	/**
	 * 获取危化品申购详细信息
	 */
	@PreAuthorize("@ss.hasPermi('labdata:casInventory:query')")
	@GetMapping(value = "/{purchaseDetailId}")
	public AjaxResult getInfo(@PathVariable("purchaseDetailId") Long purchaseDetailId) {
		return AjaxResult.success(labApplyPurchaseDetailsService.selectLabApplyPurchaseDetailsById(purchaseDetailId));
	}

	/**
	 * 新增危化品申购
	 */
	@PreAuthorize("@ss.hasPermi('labdata:casInventory:edit')")
	@Log(title = "危化品申购", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody LabApplyPurchaseDetails labApplyPurchaseDetails) {
		return toAjax(labApplyPurchaseDetailsService.insertLabApplyPurchaseDetails(labApplyPurchaseDetails));
	}
	@PreAuthorize("@ss.hasPermi('labdanger:scanin:edit')")
	@Log(title = "危化品申购入库", businessType = BusinessType.INSERT)
	@PostMapping("/scanin")
	public AjaxResult scanin(@RequestBody LabApplyPurchaseDetails labApplyPurchaseDetails) {
		return toAjax(labApplyPurchaseDetailsService.updateScanIn(labApplyPurchaseDetails));
	}
	@PreAuthorize("@ss.hasPermi('labdata:casInventory:edit')")
	@Log(title = "危化品申购采购移除状态更新", businessType = BusinessType.INSERT)
	@PostMapping("/updateRemoved")
	public AjaxResult updateRemoved(@RequestBody LabApplyPurchaseDetails labApplyPurchaseDetails) {
		return toAjax(labApplyPurchaseDetailsService.updateRemoved(labApplyPurchaseDetails));
	}
	/**
	 * 获取危化品申购详细信息
	 */
	@PreAuthorize("@ss.hasPermi('labdata:casInventory:query')")
	@GetMapping(value = "/scaninfo/{purchaseDetailId}")
	public AjaxResult getScaninInfo(@PathVariable("purchaseDetailId") Long purchaseDetailId) {
		return AjaxResult.success(labApplyPurchaseDetailsService.getScanInInfo(purchaseDetailId));
	}
	

	@PreAuthorize("@ss.hasPermi('labdata:casInventory:edit')")
	@Log(title = "化学品申购数量修改", businessType = BusinessType.UPDATE)
	@PostMapping("/updateNum")
	public AjaxResult updateNum(@RequestBody LabApplyPurchaseDetails labApplyPurchaseDetails) {
		LabApplyPurchaseDetails oldDetails = labApplyPurchaseDetailsService.selectLabApplyPurchaseDetailsById(labApplyPurchaseDetails.getPurchaseDetailId());
		//检查修改权限
		SysUser loginUser = SecurityUtils.getLoginUser().getUser();
		LabFlowTask queryTask = new LabFlowTask();
		if(oldDetails.getPurchaseId()==null){
			queryTask.setInstanceId(oldDetails.getInstanceId());
		}else{
			queryTask.setInstanceId(oldDetails.getPurchaseId());
		}
		queryTask.setCreateBy(loginUser.getUserName());
		queryTask = labFlowTaskService.checkUserTaskPermit(queryTask);
		if(queryTask==null){
			return AjaxResult.error("没有数量修改权限.");
		}
		//log
		LabFlowTaskLog labFlowTaskLog = new LabFlowTaskLog();
		labFlowTaskLog.setCreateBy(loginUser.getUserName());
		labFlowTaskLog.setCreateTime(new Date());
		labFlowTaskLog.setTaskId(queryTask.getTaskId());
		labFlowTaskLog.setInstanceId(queryTask.getInstanceId());
	
		labFlowTaskLog.setContent("数量修改："+oldDetails.getCasNo()+" 原值:"+oldDetails.getQuantity()+" 修改为:"+labApplyPurchaseDetails.getQuantity());
		return toAjax(labApplyPurchaseDetailsService.updateQuantity(labApplyPurchaseDetails, labFlowTaskLog));
	}

	//
	/**
	 * 修改危化品申购
	 */
	@PreAuthorize("@ss.hasPermi('labdata:casInventory:edit')")
	@Log(title = "危化品申购", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody LabApplyPurchaseDetails labApplyPurchaseDetails) {
		return toAjax(labApplyPurchaseDetailsService.updateLabApplyPurchaseDetails(labApplyPurchaseDetails));
	}

	/**
	 * 删除危化品申购
	 */
	@PreAuthorize("@ss.hasPermi('labdata:casInventory:edit')")
	@Log(title = "危化品申购", businessType = BusinessType.DELETE)
	@DeleteMapping("/{purchaseDetailIds}")
	public AjaxResult remove(@PathVariable Long[] purchaseDetailIds) {
		return toAjax(labApplyPurchaseDetailsService.deleteLabApplyPurchaseDetailsByIds(purchaseDetailIds));
	}
}
